home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / glibc-1.09 / glibc-1 / glibc-1.09.1 / hurd / __setauth.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-30  |  3.7 KB  |  124 lines

  1. /* Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <hurd.h>
  20. #include <hurd/port.h>
  21. #include <hurd/id.h>
  22. #include <gnu-stabs.h>
  23. #include "set-hooks.h"
  24.  
  25. /* Things in the library which want to be run when the auth port changes.  */
  26. DEFINE_HOOK (_hurd_reauth_hook, (auth_t new_auth));
  27.  
  28. #include <cthreads.h>
  29. static struct mutex reauth_lock = MUTEX_INITIALIZER;
  30.  
  31.  
  32. /* Set the auth port to NEW, and reauthenticate
  33.    everything used by the library.  */
  34. error_t
  35. _hurd_setauth (auth_t new)
  36. {
  37.   error_t err;
  38.   int d;
  39.   mach_port_t newport, ref;
  40.  
  41.   /* Give the new send right a user reference.
  42.      This is a good way to check that it is valid.  */
  43.   if (err = __mach_port_mod_refs (__mach_task_self (), new,
  44.                   MACH_PORT_RIGHT_SEND, 1))
  45.     return err;
  46.  
  47.   HURD_CRITICAL_BEGIN;
  48.  
  49.   /* We lock against another thread doing setauth.  Anyone who sets
  50.      _hurd_ports[INIT_PORT_AUTH] some other way is asking to lose.  */
  51.   __mutex_lock (&reauth_lock);
  52.  
  53.   /* Install the new port in the cell.  */
  54.   __mutex_lock (&_hurd_id.lock);
  55.   _hurd_port_set (&_hurd_ports[INIT_PORT_AUTH], new);
  56.   _hurd_id.valid = 0;
  57.   if (_hurd_id.rid_auth)
  58.     {
  59.       __mach_port_deallocate (__mach_task_self (), _hurd_id.rid_auth);
  60.       _hurd_id.rid_auth = MACH_PORT_NULL;
  61.     }
  62.   __mutex_unlock (&_hurd_id.lock);
  63.  
  64.   if (_hurd_init_dtable != NULL)
  65.     /* We just have the simple table we got at startup.
  66.        Otherwise, a reauth_hook in dtable.c takes care of this.  */
  67.     for (d = 0; d < _hurd_init_dtablesize; ++d)
  68.       if (_hurd_init_dtable[d] != MACH_PORT_NULL)
  69.     {
  70.       mach_port_t new;
  71.       ref = __mach_reply_port ();
  72.       if (! __io_reauthenticate (_hurd_init_dtable[d],
  73.                      ref, MACH_MSG_TYPE_MAKE_SEND) &&
  74.           ! HURD_PORT_USE (&_hurd_ports[INIT_PORT_AUTH],
  75.                    __auth_user_authenticate
  76.                    (port,
  77.                 _hurd_init_dtable[d],
  78.                 ref, MACH_MSG_TYPE_MAKE_SEND,
  79.                 &new)))
  80.         {
  81.           __mach_port_deallocate (__mach_task_self (),
  82.                       _hurd_init_dtable[d]);
  83.           _hurd_init_dtable[d] = new;
  84.         }
  85.       __mach_port_destroy (__mach_task_self (), ref);
  86.     }
  87.  
  88.   ref = __mach_reply_port ();
  89.   if (__USEPORT (CRDIR,
  90.          ! __io_reauthenticate (port,
  91.                     ref, MACH_MSG_TYPE_MAKE_SEND) &&
  92.          ! __auth_user_authenticate (new, port,
  93.                          ref, MACH_MSG_TYPE_MAKE_SEND,
  94.                          &newport)))
  95.     _hurd_port_set (&_hurd_ports[INIT_PORT_CRDIR], newport);
  96.   __mach_port_destroy (__mach_task_self (), ref);
  97.  
  98.   ref = __mach_reply_port ();
  99.   if (__USEPORT (CWDIR,
  100.          ! __io_reauthenticate (port,
  101.                     ref, MACH_MSG_TYPE_MAKE_SEND) &&
  102.          ! __auth_user_authenticate (new, port,
  103.                          ref, MACH_MSG_TYPE_MAKE_SEND,
  104.                          &newport)))
  105.     _hurd_port_set (&_hurd_ports[INIT_PORT_CWDIR], newport);
  106.   __mach_port_destroy (__mach_task_self (), ref);
  107.  
  108.   /* Run things which want to do reauthorization stuff.  */
  109.   RUN_HOOK (_hurd_reauth_hook, (new));
  110.  
  111.   __mutex_unlock (&reauth_lock);
  112.  
  113.   HURD_CRITICAL_END;
  114.  
  115.   return 0;
  116. }
  117.  
  118. int
  119. __setauth (auth_t new)
  120. {
  121.   error_t err = _hurd_setauth (new);
  122.   return err ? __hurd_fail (err) : 0;
  123. }
  124.